package com.hc.controller;

import com.hc.bean.PageBean;
import com.hc.bean.QueryBean;
import com.hc.constant.WeGoConst;
import com.hc.domain.Brand;
import com.hc.domain.query.BrandQuery;
import com.hc.domain.vo.BrandVO;
import com.hc.service.BrandService;
import com.hc.utils.WeGoUtil;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.time.LocalDate;

@Controller
@RequestMapping("/brand")
public class BrandController {
    @Resource
    private BrandService brandService;

    @GetMapping("/list")
    public ModelAndView list(QueryBean queryBean,
                             @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                             @RequestParam(name = "pageSize", defaultValue = WeGoConst.PAGE_SIZE) Integer pageSize,
                             ModelAndView mav) {
        // 用户请求的参数
        String param = "";
        BrandQuery brandQuery = new BrandQuery();

        final String key = queryBean.getKey();
        final String value = queryBean.getValue();
        if (StringUtils.hasText(key) && StringUtils.hasText(value)) {
            switch (key) {
                case "id":
                    param += "&key=id&value=" + value;
                    brandQuery.setId(Integer.valueOf(value));
                    break;
                case "name":
                    param += "&key=name&value=" + value;
                    brandQuery.setName(value);
                    break;
                case "info":
                    param += "&key=info&value=" + value;
                    brandQuery.setInfo(value);
                    break;
                case "status":
                    param += "&key=status&value=" + value;
                    brandQuery.setStatus(Integer.valueOf(value));
                    break;
                case "createTime":
                    param += "&key=createTime&value=" + value;
                    final String[] split = value.split(",");
                    brandQuery.setStartTime(str2LocalDate(split[0]));
                    brandQuery.setEndTime(str2LocalDate(split[1]));
                    break;
                default:
            }
        }
        PageBean<BrandVO> pageBean = brandService.getPage(brandQuery, pageNum, pageSize);
        if (param.startsWith("&")) {
            param = param.replaceFirst("&", "");
        }
        pageBean.setUrl("brand/list?" + param);

        mav.addObject("pageBean", pageBean);
        System.out.println(param);

        mav.addObject("key", key);
        mav.addObject("value", value);

        mav.setViewName("brand_list");
        return mav;
    }

    /**
     * 将字符串转换成LocalDate对象
     *
     * @param str 1999-09-21
     * @return
     */
    private LocalDate str2LocalDate(String str) {
        final String[] split = str.split("-");
        final LocalDate res = LocalDate.of(Integer.valueOf(split[0]), Integer.valueOf(split[1]), Integer.valueOf(split[2]));
        return res;
    }

    @GetMapping("/openAddPage")
    public String openAddPage() {
        return "brand_add";
    }


    @PostMapping("/add")
    public String add(HttpServletRequest request, Brand brand) {
        String fileName = null;
        try {
            fileName = WeGoUtil.uploadLogo(request, "logo123");
        } catch (IOException | ServletException e) {
            throw new RuntimeException(e);
        }
        brand.setLogo(fileName);
        brandService.insert(brand);
        return "redirect:/brand/list";
    }

    @GetMapping("/openUpdatePage")
    public ModelAndView openUpdatePage(Integer brandId, ModelAndView mav) {
        //查询
        final Brand brand = brandService.selectByPrimaryKey(brandId);
        //传递
        mav.addObject("brand", brand);
        mav.setViewName("brand_update");
        return mav;
    }

    @PostMapping("/update")
    public String update(Brand brand, HttpServletRequest request) {
        String fileName = null;
        try {
            fileName = WeGoUtil.uploadLogo(request, "logo123");
        } catch (IOException | ServletException e) {
            throw new RuntimeException(e);
        }
        brand.setLogo(fileName);
        brandService.updateByPrimaryKeySelective(brand);
        return "redirect:/brand/list";
    }

    @GetMapping("/deleteById")
    public String delete(Integer brandId) {
        brandService.deleteByPrimaryKey(brandId);
        return "redirect:/brand/list";
    }

    @GetMapping("/getById")
    public ModelAndView getById(Integer brandId,ModelAndView mav){
        final Brand brand = brandService.selectByPrimaryKey(brandId);
        mav.addObject("brand",brand);
        mav.setViewName("brand_details.ftl");
        return mav;
    }

    @ResponseBody
    @GetMapping("/getInfoById")
    public String getInfoById(Integer brandId) {
        final Brand brand = brandService.selectByPrimaryKey(brandId);
        return brand.getInfo();
    }
}
